This tutorial illustrates the core visualization utilities available in Ax.
import numpy as np
from ax.service.ax_client import AxClient
from ax.modelbridge.cross_validation import cross_validate
from ax.plot.contour import interact_contour
from ax.plot.diagnostic import interact_cross_validation
from ax.plot.scatter import(
interact_fitted,
plot_objective_vs_constraints,
tile_fitted,
)
from ax.plot.slice import plot_slice
from ax.utils.measurement.synthetic_functions import hartmann6
from ax.utils.notebook.plotting import render, init_notebook_plotting
init_notebook_plotting()
[INFO 08-29 05:59:30] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
The vizualizations require an experiment object and a model fit on the evaluated data. The routine below is a copy of the Service API tutorial, so the explanation here is omitted. Retrieving the experiment and model objects for each API paradigm is shown in the respective tutorials
noise_sd = 0.1
param_names = [f"x{i+1}" for i in range(6)] # x1, x2, ..., x6
def noisy_hartmann_evaluation_function(parameterization):
x = np.array([parameterization.get(p_name) for p_name in param_names])
noise1, noise2 = np.random.normal(0, noise_sd, 2)
return {
"hartmann6": (hartmann6(x) + noise1, noise_sd),
"l2norm": (np.sqrt((x ** 2).sum()) + noise2, noise_sd)
}
ax_client = AxClient()
ax_client.create_experiment(
name="test_visualizations",
parameters=[
{
"name": p_name,
"type": "range",
"bounds": [0.0, 1.0],
}
for p_name in param_names
],
objective_name="hartmann6",
minimize=True,
outcome_constraints=["l2norm <= 1.25"]
)
[INFO 08-29 05:59:30] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 6 decimal points.
[INFO 08-29 05:59:30] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x1. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-29 05:59:30] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x2. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-29 05:59:30] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x3. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-29 05:59:30] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x4. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-29 05:59:30] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x5. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-29 05:59:30] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x6. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-29 05:59:30] ax.service.utils.instantiation: Created search space: SearchSpace(parameters=[RangeParameter(name='x1', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x2', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x3', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x4', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x5', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x6', parameter_type=FLOAT, range=[0.0, 1.0])], parameter_constraints=[]).
[INFO 08-29 05:59:30] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters.
[INFO 08-29 05:59:30] ax.modelbridge.dispatch_utils: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 12 trials, GPEI for subsequent trials]). Iterations after 12 will take longer to generate due to model-fitting.
for i in range(20):
parameters, trial_index = ax_client.get_next_trial()
# Local evaluation here can be replaced with deployment to external system.
ax_client.complete_trial(trial_index=trial_index, raw_data=noisy_hartmann_evaluation_function(parameters))
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.302218, 'x2': 0.971222, 'x3': 0.050901, 'x4': 0.076947, 'x5': 0.153494, 'x6': 0.682365}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.101938, 0.1), 'l2norm': (1.121783, 0.1)}.
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.22303, 'x2': 0.764466, 'x3': 0.213129, 'x4': 0.760623, 'x5': 0.576837, 'x6': 0.582341}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.241254, 0.1), 'l2norm': (1.332916, 0.1)}.
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.852595, 'x2': 0.475635, 'x3': 0.344208, 'x4': 0.276618, 'x5': 0.85003, 'x6': 0.460206}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.115413, 0.1), 'l2norm': (1.51839, 0.1)}.
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.348276, 'x2': 0.029343, 'x3': 0.706402, 'x4': 0.157281, 'x5': 0.540136, 'x6': 0.409798}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.727271, 0.1), 'l2norm': (1.104301, 0.1)}.
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.970152, 'x2': 0.463599, 'x3': 0.639784, 'x4': 0.621678, 'x5': 0.835126, 'x6': 0.981208}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (0.245351, 0.1), 'l2norm': (2.004761, 0.1)}.
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.682331, 'x2': 0.39598, 'x3': 0.429293, 'x4': 0.38662, 'x5': 0.768929, 'x6': 0.870976}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (0.076467, 0.1), 'l2norm': (1.368511, 0.1)}.
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.444333, 'x2': 0.879943, 'x3': 0.048345, 'x4': 0.781594, 'x5': 0.97887, 'x6': 0.538074}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (0.091143, 0.1), 'l2norm': (1.594002, 0.1)}.
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.908919, 'x2': 0.146084, 'x3': 0.247055, 'x4': 0.250735, 'x5': 0.078689, 'x6': 0.491982}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-0.194691, 0.1), 'l2norm': (1.058006, 0.1)}.
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.519011, 'x2': 0.974371, 'x3': 0.753444, 'x4': 0.490989, 'x5': 0.37555, 'x6': 0.80448}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.141336, 0.1), 'l2norm': (1.678257, 0.1)}.
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.084316, 'x2': 0.40168, 'x3': 0.332088, 'x4': 0.347953, 'x5': 0.875257, 'x6': 0.035545}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (0.01006, 0.1), 'l2norm': (1.064083, 0.1)}.
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.446452, 'x2': 0.90722, 'x3': 0.411735, 'x4': 0.572663, 'x5': 0.402586, 'x6': 0.077171}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-2.942975, 0.1), 'l2norm': (1.381589, 0.1)}.
[INFO 08-29 05:59:30] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.652896, 'x2': 0.813055, 'x3': 0.436292, 'x4': 0.164082, 'x5': 0.687471, 'x6': 0.062221}.
[INFO 08-29 05:59:30] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.140139, 0.1), 'l2norm': (1.282103, 0.1)}.
[INFO 08-29 06:00:06] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.284551, 'x2': 0.81641, 'x3': 0.348817, 'x4': 0.524614, 'x5': 0.403852, 'x6': 0.053158}.
[INFO 08-29 06:00:06] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-2.301122, 0.1), 'l2norm': (1.102171, 0.1)}.
[INFO 08-29 06:01:25] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.335947, 'x2': 0.86585, 'x3': 0.379366, 'x4': 0.555874, 'x5': 0.394259, 'x6': 0.062544}.
[INFO 08-29 06:01:25] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-2.762917, 0.1), 'l2norm': (1.256631, 0.1)}.
[INFO 08-29 06:03:07] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.220788, 'x2': 0.919246, 'x3': 0.392737, 'x4': 0.587189, 'x5': 0.378444, 'x6': 0.064732}.
[INFO 08-29 06:03:07] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-1.783367, 0.1), 'l2norm': (1.213081, 0.1)}.
[INFO 08-29 06:04:04] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.405955, 'x2': 0.843607, 'x3': 0.314762, 'x4': 0.536354, 'x5': 0.380275, 'x6': 0.053153}.
[INFO 08-29 06:04:04] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-3.028267, 0.1), 'l2norm': (1.206598, 0.1)}.
[INFO 08-29 06:04:55] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.396561, 'x2': 0.737, 'x3': 0.327836, 'x4': 0.539892, 'x5': 0.346282, 'x6': 0.053018}.
[INFO 08-29 06:04:55] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-2.419334, 0.1), 'l2norm': (1.277045, 0.1)}.
[INFO 08-29 06:05:17] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.401637, 'x2': 0.905796, 'x3': 0.28589, 'x4': 0.479579, 'x5': 0.388262, 'x6': 0.044123}.
[INFO 08-29 06:05:17] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-2.906543, 0.1), 'l2norm': (1.146224, 0.1)}.
[INFO 08-29 06:06:07] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.412578, 'x2': 0.888846, 'x3': 0.284284, 'x4': 0.556404, 'x5': 0.416234, 'x6': 0.013774}.
[INFO 08-29 06:06:07] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-3.133703, 0.1), 'l2norm': (1.175016, 0.1)}.
[INFO 08-29 06:06:50] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.42124, 'x2': 0.918801, 'x3': 0.292302, 'x4': 0.582067, 'x5': 0.375796, 'x6': 0.0}.
[INFO 08-29 06:06:50] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-3.129895, 0.1), 'l2norm': (1.233339, 0.1)}.
The plot below shows the response surface for hartmann6 metric as a function of the x1, x2 parameters.
The other parameters are fixed in the middle of their respective ranges, which in this example is 0.5 for all of them.
# this could alternately be done with `ax.plot.contour.plot_contour`
render(ax_client.get_contour_plot(param_x="x1", param_y="x2", metric_name='hartmann6'))
[INFO 08-29 06:06:50] ax.service.ax_client: Retrieving contour plot with parameter 'x1' on X-axis and 'x2' on Y-axis, for metric 'hartmann6'. Remaining parameters are affixed to the middle of their range.
The plot below allows toggling between different pairs of parameters to view the contours.
model = ax_client.generation_strategy.model
render(interact_contour(model=model, metric_name='hartmann6'))
This plot illustrates the tradeoffs achievable for 2 different metrics. The plot takes the x-axis metric as input (usually the objective) and allows toggling among all other metrics for the y-axis.
This is useful to get a sense of the pareto frontier (i.e. what is the best objective value achievable for different bounds on the constraint)
render(plot_objective_vs_constraints(model, 'hartmann6', rel=False))
CV plots are useful to check how well the model predictions calibrate against the actual measurements. If all points are close to the dashed line, then the model is a good predictor of the real data.
cv_results = cross_validate(model)
render(interact_cross_validation(cv_results))
Slice plots show the metric outcome as a function of one parameter while fixing the others. They serve a similar function as contour plots.
render(plot_slice(model, "x2", "hartmann6"))
Tile plots are useful for viewing the effect of each arm.
render(interact_fitted(model, rel=False))
Total runtime of script: 7 minutes, 46.18 seconds.